//
//  ViewController.swift
//  AliIndexEffect
//
//  Created by 陈钟 on 2019/12/9.
//  Copyright © 2019 陈钟. All rights reserved.
//

import UIKit
import AliIndexEffectFramework

/// 状态栏高度
func statusBarHeight() -> CGFloat {
    if #available(iOS 13, *) {
        return UIApplication.shared.windows.last?.windowScene?.statusBarManager?.statusBarFrame.size.height ?? 0
    } else {
        return UIApplication.shared.statusBarFrame.size.height
    }
}

/// navigationBar 高度
func navigationBarHeight() -> CGFloat {
    return statusBarHeight() + 44
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        self.tableView.frame = self.view.bounds
        self.view.addSubview(self.tableView)



        self.tableView.zz_topView = self.topView
        self.tableView.zz_topViewHeight = 200
        self.tableView.zz_topViewShowHeight = 50

        let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 200))
        headerView.backgroundColor = UIColor.orange
        self.tableView.tableHeaderView = headerView

        self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                [weak self] in
                guard let `self` = self else { return }
                self.tableView.mj_header?.endRefreshing()
            }
        })

        self.tableView.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                [weak self] in
                guard let `self` = self else { return }
                self.tableView.mj_footer?.endRefreshing()
            }
        })
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

//        self.tableView.frame = self.view.bounds
        self.tableView.frame = CGRect(x: 0, y: navigationBarHeight(), width: self.view.frame.width, height: self.view.frame.height - navigationBarHeight())
    }

    lazy var topView: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.red
        return view
    }()
    lazy var tableView: UITableView = {
        let tab = UITableView.init(frame: CGRect.zero, style: .grouped)
        tab.contentInsetAdjustmentBehavior = .never
        tab.estimatedRowHeight = 0
        tab.estimatedSectionFooterHeight = 0
        tab.estimatedSectionHeaderHeight = 0
        tab.delegate = self
        tab.dataSource = self
        return tab
    }()

}
extension ViewController: UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UILabel()
        view.backgroundColor = UIColor.yellow
        view.text = "section - \(section)"
        return view
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell : UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "AddressSearchList_Cell")
        if cell == nil {
            cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "AddressSearchList_Cell")
        }
        cell?.textLabel?.text = "you are good man"
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        print("offsetY = \(offsetY)")
        if offsetY > 0 && offsetY < 139 {
            self.topView.alpha = 1 - ((offsetY - 89) / 50)
        }else if offsetY >= 139 {
            self.topView.alpha = 0
        }else{
            self.topView.alpha = 1.0
        }

    }
}

